/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.actions.create;

import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;

import com.cea.papyrus.core.editor.ModelManager;

/**
 * Action to create a UML element and to add it to a owner element
 */
public class CreateElementAction extends SelectionAction {

	/** Request passed to the owner edit part */
	public final static String CREATE_ELEMENT_ACTION_REQUEST = "createelementrequest";
	
	/** Type of element to create */
	public final static String ELEMENT_KEY = "elementToCreate";

	/** ID of this action*/
	public final static String CREATE_ELEMENT_ACTION_ID = "createelement";

	/** Request created by this action */
	protected Request request;

	/**
	 * Creates a new CreateElementAction.
	 * @param part the workbench part which gives the selection
	 * @param modelManager the model manager associated to the current editor
	 * @param elementClass type of element to create
	 * @param type current diagram type
	 */
	public CreateElementAction(IWorkbenchPart part, Class elementClass, ModelManager modelManager, String type) {
		super(part);
		request = new Request(CREATE_ELEMENT_ACTION_REQUEST);
		request.getExtendedData().put(ELEMENT_KEY, elementClass);
		setText(elementClass.getName().substring(elementClass.getName().lastIndexOf('.')+1));
		setId(CREATE_ELEMENT_ACTION_ID);
		setToolTipText("creates a "+elementClass.getName().substring(elementClass.getName().lastIndexOf('.')+1)+" in the model");
		ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
		setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD));
		setHoverImageDescriptor(getImageDescriptor());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.action.Action#run()
	 */
	@Override
	public void run () {
		BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {
			public void run() {
				Command command = getCommand();
				if(command != null) {
					execute(command);
				}
			}
		});
	}
	
	/**
	 * Returns the command that creates the element and adds it to the parent edit part 
	 * @return the creation element command
	 */
	protected Command getCommand() {
		// Select the first edit part to choose the editpart
		List<Object> editParts = getSelectedObjects();
		
		// do the job for the first selected element
		for (int i=0; i<editParts.size(); i++){
			Object o = editParts.get(i);
			if(o instanceof EditPart) {
				return ((EditPart)o).getCommand(request);
			}
		}
		return null;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.ui.actions.WorkbenchPartAction#calculateEnabled()
	 */
	@Override
	protected boolean calculateEnabled() {
		if(getCommand() != null) {
			return getCommand().canExecute();
		} else {
			return false;
		}
	}

}
